//
//  DeviceInterface.cpp
//
//  Copyright (C) 2007 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//

#include <windows.h>
#include <pm.h>
#include <winioctl.h>
#include <pwingdi.h>
#include "DeviceInterface.h"
#include "progbar.h"
//==================================================
BOOL StreamDeviceInit()
{
	return TRUE;
}
HANDLE StreamDeviceOpen(TCHAR * pszname)
{
	HANDLE hRet = CreateFile(pszname, 0, 
        FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

	return hRet;
}
BOOL StreamDeviceClose(HANDLE hdev)
{
	CloseHandle(hdev);
	return TRUE;
}	
BOOL StreamDeviceRequest(HANDLE hdev, DWORD dwRequest, LPVOID pInBuf, DWORD dwInSize, 
              LPVOID pOutBuf, DWORD dwOutSize, LPDWORD pdwBytesRet)
{
	return DeviceIoControl(hdev, dwRequest, pInBuf, dwInSize, 
            pOutBuf, dwOutSize, pdwBytesRet, NULL);
}             
struct dev_opt g_StreamOpt=
{
	StreamDeviceInit,
	StreamDeviceOpen,
	StreamDeviceClose,
	StreamDeviceRequest
};
//==================================================
BOOL DisplayDeviceInit()
{
	return TRUE;
}
HANDLE DisplayDeviceOpen(TCHAR * pszname)
{
	HANDLE hRet = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);

	if(hRet==NULL)
	{
		hRet=INVALID_HANDLE_VALUE;
	}
	else
	{
        // determine whether the display driver really supports the PM IOCTLs
        DWORD dwIoctl[] = { IOCTL_POWER_CAPABILITIES, IOCTL_POWER_SET, IOCTL_POWER_GET };
        int i;
        for(i = 0; i < dim(dwIoctl); i++) 
		{
            if(ExtEscape((HDC) hRet, QUERYESCSUPPORT, sizeof(dwIoctl[i]), (LPCSTR) &dwIoctl[i], 0, NULL) <= 0) 
			{
                break;
            }
        }
        if(i < dim(dwIoctl)) 
		{
            DeleteDC((HDC) hRet);
            hRet = INVALID_HANDLE_VALUE;
		}
	}

	return hRet;
}
BOOL DisplayDeviceClose(HANDLE hdev)
{
	DeleteDC((HDC) hdev);
	return TRUE;
}	
BOOL DisplayDeviceRequest(HANDLE hdev, DWORD dwRequest, LPVOID pInBuf, DWORD dwInSize, 
              LPVOID pOutBuf, DWORD dwOutSize, LPDWORD pdwBytesRet)
{
	return ExtEscape((HDC) hdev, dwRequest, dwInSize, (LPCSTR) pInBuf, 
            dwOutSize, (LPSTR) pOutBuf);
}             
struct dev_opt g_DisplayOpt=
{
	DisplayDeviceInit,
	DisplayDeviceOpen,
	DisplayDeviceClose,
	DisplayDeviceRequest
};
//==================================================